﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Events.*;
    import flash.utils.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.CollectionsUI.*;

    public class Collection extends GameSettingsObject {

        private static const COMPLETED:String = "COMPLETED_THE_COLLECTION";

        private static var s_totalChestsFound:int = 0;
        private static var s_openedChestsForCollection:Dictionary;
        private static var s_itemToCollectionMapping:Dictionary;

        private var openedChests:int = 0;
        private var m_totalItemsInCollection:int = 0;
        private var m_cachedWishlistableCount:int = -1;

        public function Collection(_arg1:XML){
            super(_arg1);
        }
        public static function collectInfo(_arg1:XML):void{
            var _local3:int;
            var _local4:XMLList;
            var _local5:XML;
            var _local6:Collection;
            var _local7:Boolean;
            var _local8:String;
            var _local9:int;
            if (Collection.s_openedChestsForCollection == null){
                Collection.s_openedChestsForCollection = new Dictionary();
                Collection.s_itemToCollectionMapping = new Dictionary();
            };
            var _local2:String = _arg1.@name;
            if (Collection.s_openedChestsForCollection[_local2] == null){
                Collection.s_openedChestsForCollection[_local2] = new Dictionary();
                _local3 = 0;
                _local4 = _arg1.collectables.children();
                for each (_local5 in _local4) {
                    _local8 = String(_local5.@name);
                    Collection.s_itemToCollectionMapping[_local8] = _local2;
                    _local9 = Global.player.getNumCollectablesOwned(_local8);
                    if (_local9 > 0){
                        Collection.s_openedChestsForCollection[_local2][_local8] = _local9;
                        _local3 = (_local3 + 1);
                        Collection.s_totalChestsFound = (Collection.s_totalChestsFound + 1);
                    };
                };
                _local6 = new Collection(_arg1);
                _local7 = _local6.isReadyToTradeIn();
                Collection.s_openedChestsForCollection[_local2][COMPLETED] = _local7;
            };
        }
        public static function completedEarlier(_arg1:String):Boolean{
            if ((((s_openedChestsForCollection == null)) || ((s_openedChestsForCollection[_arg1] == null)))){
                return (false);
            };
            return (s_openedChestsForCollection[_arg1][COMPLETED]);
        }
        public static function setCompletedEarlier(_arg1:String, _arg2:Boolean):void{
            if ((((s_openedChestsForCollection == null)) || ((s_openedChestsForCollection[_arg1] == null)))){
                return;
            };
            s_openedChestsForCollection[_arg1][COMPLETED] = _arg2;
        }
        public static function get totalChestsFound():int{
            return (s_totalChestsFound);
        }
        public static function getCollectionNameFromItemName(_arg1:String):String{
            if ((((s_itemToCollectionMapping == null)) || ((s_itemToCollectionMapping[_arg1] == null)))){
                Global.gameSettings.getCollectionByCollectableName(_arg1);
            };
            var _local2:String = s_itemToCollectionMapping[_arg1];
            return (_local2);
        }
        public static function getFoundItemsByCollectionName(_arg1:String):int{
            var _local3:String;
            if (_arg1 == null){
                return (0);
            };
            var _local2:int;
            for (_local3 in s_openedChestsForCollection[_arg1]) {
                _local2 = (_local2 + 1);
            };
            return (_local2);
        }
        public static function addCollectionToPlayer(_arg1:String, _arg2:Boolean=true):void{
            var _local3:String = getCollectionNameFromItemName(_arg1);
            var _local4:Boolean = Global.player.hasCompletedCollection(_local3);
            Global.player.addCollectable(_arg1);
            var _local5:Boolean = Global.player.hasCompletedCollection(_local3);
            if (((((!(_local4)) && (_local5))) && (_arg2))){
                Sounds.play("collection_completed");
            };
            if (Global.ui.collectionView != null){
                (Global.ui.collectionView.asset.shelf as CollectionScrollingList).invalidateData();
            };
        }

        override protected function getFriendlyNameLocFile():String{
            return ("Collections");
        }
        public function getRewardXml():XMLList{
            var oneTimeReward:* = null;
            if (m_xml.hasOwnProperty("oneTimeRewards")){
                oneTimeReward = m_xml.oneTimeRewards.oneTimeReward.(@count == Global.player.numTradeIns(this.name));
                if (oneTimeReward.length() > 0){
                    return (oneTimeReward);
                };
            };
            return (m_xml.tradeInReward);
        }
        public function get rewardCommodities():Array{
            var _local3:XML;
            var _local4:Object;
            var _local1:XMLList = this.getRewardXml();
            var _local2:Array = new Array();
            if ((_local1.goods as XMLList).length() > 0){
                _local3 = _local1.goods[0];
                _local4 = new Object();
                _local4.name = "goods";
                _local4.amount = parseInt(_local3.@amount);
                _local2.push(_local4);
            };
            return (_local2);
        }
        public function get rewardItemNames():Array{
            var _local3:XML;
            var _local1:XMLList = this.getRewardXml();
            var _local2:Array = new Array();
            for each (_local3 in _local1.item) {
                _local2.push(new String(_local3.@name));
            };
            for each (_local3 in _local1.rewardItem) {
                _local2.push(new String(_local3.@name));
            };
            return (_local2);
        }
        public function get rewardCollectableNames():Array{
            var _local3:XML;
            var _local1:XMLList = this.getRewardXml();
            var _local2:Array = new Array();
            for each (_local3 in _local1.collectable) {
                _local2.push(new String(_local3.@name));
            };
            return (_local2);
        }
        public function get rewardXp():int{
            var _local3:XML;
            var _local1:XMLList = this.getRewardXml();
            var _local2:int;
            for each (_local3 in _local1.xp) {
                _local2 = (_local2 + parseInt(_local3.@amount));
            };
            return (_local2);
        }
        public function get rewardCoins():int{
            var _local3:XML;
            var _local1:XMLList = this.getRewardXml();
            var _local2:int;
            for each (_local3 in _local1.coin) {
                _local2 = (_local2 + parseInt(_local3.@amount));
            };
            return (_local2);
        }
        public function get rewardEnergy():int{
            var _local3:XML;
            var _local1:XMLList = this.getRewardXml();
            var _local2:int;
            for each (_local3 in _local1.energy) {
                _local2 = (_local2 + parseInt(_local3.@amount));
            };
            return (_local2);
        }
        public function get rewardUnlocks():Array{
            var _local3:XML;
            var _local1:XMLList = this.getRewardXml();
            var _local2:Array = new Array();
            for each (_local3 in _local1.itemUnlock) {
                _local2.push(new String(_local3.@name));
            };
            return (_local2);
        }
        public function get icon():String{
            return (Global.getAssetURL(m_xml.icon.@url));
        }
        public function getTotalCollectablesCount():int{
            var _local1:XMLList = m_xml.collectables.children();
            return (_local1.length());
        }
        public function hasPlayerCompleted():Boolean{
            return (Global.player.hasCompletedCollection(name));
        }
        public function getCurrentUniqueCollectablesCount():int{
            var _local3:XML;
            var _local1:int;
            if (m_xml == null){
                return (_local1);
            };
            var _local2:XMLList = m_xml.collectables.children();
            for each (_local3 in _local2) {
                if (Global.player.getNumCollectablesOwned(_local3.@name) > 0){
                    _local1++;
                };
            };
            return (_local1);
        }
        public function getUniqueCollectablesCountForNextTradeIn():int{
            var _local4:XML;
            var _local5:Boolean;
            var _local6:int;
            var _local1:int;
            var _local2:Array = new Array();
            if (m_xml == null){
                return (_local1);
            };
            var _local3:XMLList = m_xml.collectables.children();
            for each (_local4 in _local3) {
                _local2.push(Global.player.getNumCollectablesOwned(_local4.@name));
            };
            _local5 = false;
            while (!(_local5)) {
                _local6 = 0;
                while (_local6 < _local2.length) {
                    if (_local2[_local6] != 0){
                        var _local7 = _local2;
                        var _local8 = _local6;
                        var _local9 = (_local7[_local8] - 1);
                        _local7[_local8] = _local9;
                    } else {
                        _local5 = true;
                        _local1++;
                    };
                    _local6++;
                };
            };
            return ((this.getTotalCollectablesCount() - _local1));
        }
        public function getCollectablesXMLList():XMLList{
            var _local1:XMLList = m_xml.collectables.children();
            return (_local1);
        }
        public function getCollectableNames():Array{
            var _local3:XML;
            var _local1:Array = [];
            var _local2:XMLList = m_xml.collectables.children();
            for each (_local3 in _local2) {
                _local1.push(String(_local3.@name));
            };
            return (_local1);
        }
        public function getCollectableXML(_arg1:String):XML{
            var _local3:XML;
            var _local2:XMLList = m_xml.collectables.children();
            for each (_local3 in _local2) {
                if (_local3.@name == _arg1){
                    return (_local3);
                };
            };
            return (null);
        }
        public function isReadyToTradeIn():Boolean{
            return ((this.getTotalCollectablesCount() == this.getCurrentUniqueCollectablesCount()));
        }
        public function tradeIn():Boolean{
            var _local2:XML;
            if (this.getTotalCollectablesCount() != this.getCurrentUniqueCollectablesCount()){
                return (false);
            };
            var _local1:XMLList = m_xml.collectables.children();
            for each (_local2 in _local1) {
                Global.player.removeCollectable(_local2.@name);
            };
            Global.player.completeCollection(this.name);
            this.grantReward();
            if (this.shouldTrackTradeIns()){
                Global.player.trackTradeIns(this.name);
            };
            GameTransactionManager.addTransaction(new TCollectionTradeIn(this.name), true);
            var _local3:Boolean = this.isReadyToTradeIn();
            Collection.setCompletedEarlier(this.name, _local3);
            return (true);
        }
        protected function shouldTrackTradeIns():Boolean{
            return (m_xml.hasOwnProperty("oneTimeRewards"));
        }
        public function completedCollection(_arg1:String):Boolean{
            var collectable:* = null;
            var numOwned:* = 0;
            var nameOfAddedItem:* = _arg1;
            var collectables:* = m_xml.collectables.children();
            if (collectables.(@name == nameOfAddedItem).length() != 1){
                return (false);
            };
            var countOfAddedItem:* = Global.player.getNumCollectablesOwned(nameOfAddedItem);
            if (countOfAddedItem == 0){
                return (false);
            };
            for each (collectable in collectables) {
                this.m_totalItemsInCollection = (this.m_totalItemsInCollection + 1);
                numOwned = Global.player.getNumCollectablesOwned(collectable.@name);
                if ((((numOwned == 0)) || ((numOwned < countOfAddedItem)))){
                    return (false);
                };
            };
            return (true);
        }
        public function incrementOpenedChestCounter(_arg1:String):void{
            Collection.s_openedChestsForCollection[name][_arg1] = -1;
            Collection.s_totalChestsFound = (Collection.s_totalChestsFound + 1);
        }
        public function completedCollectionAndOpenedAllChests(_arg1:String):Boolean{
            var _local3:Object;
            if (!this.completedCollection(_arg1)){
                return (false);
            };
            var _local2:int;
            for (_local3 in Collection.s_openedChestsForCollection[name]) {
                _local2 = (_local2 + 1);
            };
            if (_local2 == this.m_totalItemsInCollection){
                return (true);
            };
            return (false);
        }
        public function getNumWishlistableItems(_arg1:Boolean=false):int{
            var _local3:String;
            var _local4:Item;
            if (((_arg1) && ((this.m_cachedWishlistableCount >= 0)))){
                return (this.m_cachedWishlistableCount);
            };
            var _local2:int;
            for each (_local3 in this.getCollectableNames()) {
                _local4 = Global.gameSettings.getItemByCode(_local3);
                if (!((Global.player.isItemOnWishlist(_local4.name)) || ((Global.player.getNumCollectablesOwned(_local3) > 0)))){
                    _local2++;
                };
            };
            this.m_cachedWishlistableCount = _local2;
            return (_local2);
        }
        private function grantReward():void{
            var _local5:String;
            var _local6:Object;
            var _local7:String;
            var _local8:String;
            Global.player.xp = (Global.player.xp + this.rewardXp);
            Global.player.gold = (Global.player.gold + this.rewardCoins);
            Global.player.updateEnergy(this.rewardEnergy, new Array("energy", "earnings", "rewards", ""));
            var _local1:Array = this.rewardCommodities;
            if (((_local1) && ((_local1.length > 0)))){
                for each (_local6 in _local1) {
                    Global.player.commodities.add(_local6.name, _local6.amount);
                };
                Global.world.dispatchEvent(new FarmGameWorldEvent(FarmGameWorldEvent.USER_CHANGED));
            };
            var _local2:Array = this.rewardItemNames;
            if (((_local2) && ((_local2.length > 0)))){
                for each (_local7 in _local2) {
                    Global.player.inventory.addItems(_local7, 1);
                };
                Global.world.dispatchEvent(new FarmGameWorldEvent(FarmGameWorldEvent.USER_CHANGED));
            };
            var _local3:Array = this.rewardCollectableNames;
            if (((_local3) && ((_local3.length > 0)))){
                for each (_local8 in _local3) {
                    Global.player.addCollectable(_local8);
                };
                Global.world.dispatchEvent(new FarmGameWorldEvent(FarmGameWorldEvent.USER_CHANGED));
            };
            var _local4:Array = this.rewardUnlocks;
            for each (_local5 in _local4) {
                Global.player.setSeenFlag(_local5);
                Global.player.setSeenSessionFlag(_local5);
            };
        }
        public function getRarity():String{
            var _local1:String;
            var _local2:String;
            if (this.xml.@rarity.length > 0){
                _local1 = this.xml.@rarity;
                switch (_local1){
                    case "common":
                        _local2 = ZLoc.t("Dialogs", "CommonFriendly");
                        break;
                    case "uncommon":
                        _local2 = ZLoc.t("Dialogs", "UncommonFriendly");
                        break;
                    case "rare":
                        _local2 = ZLoc.t("Dialogs", "RareFriendly");
                        break;
                    case "ultrarare":
                        _local2 = ZLoc.t("Dialogs", "UltraRareFriendly");
                        break;
                    default:
                        _local2 = "";
                };
                return (_local2);
            };
            return ("No rarity");
        }

    }
}//package Classes 
